﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Theming;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;

namespace OpenRPOSBackOffice
{
    public partial class Login : Page
    {
        public Login()
        {
            InitializeComponent();
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private async void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            try
            {

                // Show the wait cursor
                WaitCursor.Visibility = Visibility.Visible;
                WaitCursor.Width = Globals.mainPage.LayoutRoot.Width;
                WaitCursor.Height = Globals.mainPage.LayoutRoot.Height;
                // Disable buttons
                btnLogin.IsEnabled = false;
                tbUsername.IsEnabled = false;
                pbPassword.IsEnabled = false;
                Globals.token = (await Globals.db.Login(tbUsername.Text, pbPassword.Password));
                // Determine whether or not login was successful
                if (Globals.token <= 0)
                {
                    Globals.login.titleMessage.Visibility = Visibility.Visible;
                    Globals.login.titleMessage.Content = Globals.k_str_invalid_login;
                }
                else
                {
                    Globals.login.titleMessage.Visibility = Visibility.Collapsed;
                    // Get the current username
                    Globals.username = Globals.login.tbUsername.Text;
                    // Clear username/password fields
                    Globals.login.tbUsername.Text = "";
                    Globals.login.pbPassword.Password = "";
                    Globals.loginID = await Globals.db.GetLoginID();
                    Globals.currentEmployee = null;
                    // Retrieve current account information
                    if (await Globals.db.GetEmployeeObjects("LoginID=" + Globals.loginID.ToString()))
                        if(Globals.db.EmployeeRecords.Count==1) // Be sure we got exactly one!
                            Globals.currentEmployee = Globals.db.EmployeeRecords[0];
                    // Retrieve current login information
                    if(await Globals.db.GetLoginObjects("ID=" + Globals.loginID.ToString()))
                        if(Globals.db.LoginRecords.Count==1)
                            Globals.currentLogin=Globals.db.LoginRecords[0];
                    // Go to main screen
                    Globals.root.Content = Globals.mainPage;
                    Globals.mainPage.init();
                }
            }
            catch (Exception ex)
            {
                titleMessage.Visibility = Visibility.Visible;
                titleMessage.Content = ex.Message;

            }
            finally
            {
                // Reset the cursor
                WaitCursor.Visibility = Visibility.Collapsed;
                // Enable buttons
                btnLogin.IsEnabled = true;
                tbUsername.IsEnabled = true;
                pbPassword.IsEnabled = true;
            }
        }

        private void pbPassword_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key != Key.Enter)
                return;
            e.Handled = true;
            btnLogin_Click(sender, e);
        }

    }
}
